﻿namespace Utility1
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;

    public class SiteNavigation
    {
        public const string controlMenuBar = "~/admin/Pages/MenuBar.ascx";
        public const string controlMenuSubBar = "~/admin/Pages/MenuSubBar.ascx";
        public const string controlQuangcaosp = "~/admin/Pages/Quanly_Quangcao.ascx";
        public const string contronl_Menu = "~/admin/Pages/Menu.ascx";
        public const string contronl_MenuCon = "~/admin/Pages/MenuCon.ascx";
        public const string contronl_Product = "~/admin/Pages/editproduct.ascx";
        public const string contronl_QuanlySP = "~/admin/Pages/quanlySanpham.ascx";
        public const string contronlHome = "~/admin/Pages/Home.ascx";
        public const string contronlKichhoat = "~/admin/Pages/kichhoat.ascx";
        public const string contronlSodo = "~/admin/Pages/sodo.ascx";
        public const string contronlThanhvien = "~/admin/Pages/Thanhvien.ascx";
        public const string contronlthongke = "~/admin/Pages/thongke.ascx";
  
        public const string enrollmentPage = "~/Pages/Enroll.ascx";
        public const string link_Menu = "~/admin/index.aspx?page=menu";
        public const string link_thongke = "~/admin/index.aspx?page=thong-ke";
        public const string link_MenuBar = "~/admin/index.aspx?page=MenuBar";
        public const string link_MenuCon = "~/admin/index.aspx?page=MenuCon";
        public const string link_MenuSubBar = "~/admin/index.aspx?page=MenuBarSub";
        public const string link_Product = "~/admin/index.aspx?page=edit-product";
        public const string link_QuanlySP = "~/admin/index.aspx?page=QuanlySP";
        public const string link_Quangcaosp = "~/admin/index.aspx?page=quanly-quangcao";
        public const string linkAdminHome = "~/admin/index.aspx?page=home";
        public const string linkKichhoat = "~/admin/index.aspx?page=Kich-hoat";
        public const string linkSodo = "~/admin/index.aspx?page=So-do";
        public const string linkThanhvien = "~/admin/index.aspx?page=Thanh-vien";
        public const string LoginPage = "~/admin/login.aspx";
        private const string slash = "/";
        protected string templatePath = string.Empty;

        public static string AppendQueryString(string url, string queryString)
        {
            return string.Format("{0}{1}{2}", url, (url.IndexOf('?') >= 0) ? "&" : "?", queryString);
        }

        public static string Combine(string path1, string path2)
        {
            return (path1 + "/" + path2);
        }

        public static string CompleteHttpUrl(bool isSSL, params string[] partialUrls)
        {
            int port = HttpContext.Current.Request.Url.Port;
            if ((80 != port) && (0x1bb != port))
            {
                partialUrls[0] = string.Format("{0}:{1}", partialUrls[0], port);
            }
            string str = Regex.Replace(Regex.Replace(string.Join("/", partialUrls), @"\/+", "/"), @"\/$", "");
            return ("http" + (isSSL ? "s" : "") + "://" + str);
        }

        public static string ConcatenatePartsToUrl(params string[] names)
        {
            StringBuilder builder = new StringBuilder();
            foreach (string str in names)
            {
                if (!string.IsNullOrEmpty(str))
                {
                    if (0 == builder.Length)
                    {
                        builder.Append(str);
                    }
                    else
                    {
                        builder.Append("/").Append(str);
                    }
                }
            }
            return builder.ToString();
        }

        private static string GetApplicationPath()
        {
            if ("/" == HttpContext.Current.Request.ApplicationPath)
            {
                return "";
            }
            return HttpContext.Current.Request.ApplicationPath;
        }

        public static string GetFirstPath(string urlPath)
        {
            Match match = Regex.Match(urlPath, @"^\/?(?<firstPath>[^\/]+)\/");
            if (match.Success)
            {
                return match.Groups["firstPath"].Value;
            }
            return string.Empty;
        }

        public static string GetRelativePathToApplicatonRoot(string fullPath)
        {
            string applicationPath = GetApplicationPath();
            if (0 == applicationPath.Length)
            {
                return RemoveHeadingSlash(fullPath);
            }
            Match match = Regex.Match(fullPath, @"^\/?" + applicationPath + "(.*)$", RegexOptions.IgnoreCase);
            if (match.Success)
            {
                return RemoveHeadingSlash(match.Groups[1].Value);
            }
            return fullPath;
        }

        private static string GetStore(string urlPath)
        {
            return GetFirstPath(urlPath);
        }

        private static string MigrateRewriteUrl(string page)
        {
            if (page.Equals("product", StringComparison.OrdinalIgnoreCase) || page.Equals("category", StringComparison.OrdinalIgnoreCase))
            {
                return string.Format("Store/{0}", page);
            }
            return page;
        }

        private static string RemoveHeadingSlash(string path)
        {
            if (path.StartsWith("/"))
            {
                return path.Substring(1);
            }
            return path;
        }

        public static string RemoveHostFromUrl(string fullPathWithHost)
        {
            Match match = Regex.Match(fullPathWithHost, @"^https?:\/\/[^\/]+(.*)$", RegexOptions.IgnoreCase);
            if (match.Success)
            {
                return match.Groups[1].Value;
            }
            return fullPathWithHost;
        }

        public static string ResolveUrl(string relativePathToApplicationRoot)
        {
            return (GetApplicationPath() + "/" + relativePathToApplicationRoot);
        }

        public static string ResolveUrlInText(string text)
        {
            return text.Replace("~", GetApplicationPath());
        }

        public static string UnMapPath(string filespec)
        {
            string physicalApplicationPath = HttpContext.Current.Request.PhysicalApplicationPath;
            if (!filespec.StartsWith(physicalApplicationPath, StringComparison.OrdinalIgnoreCase))
            {
                throw new ApplicationException(string.Format("{0} is not inside application path {1}.", filespec, physicalApplicationPath));
            }
            return ResolveUrl(RemoveHeadingSlash(filespec.Substring(physicalApplicationPath.Length).Replace('\\', '/')));
        }

        public static string UpdateUrl(string url, Dictionary<string, string> queryStringReplacement, ICollection<string> queryStringRemoval)
        {
            string str;
            string str2;
            int index = url.IndexOf('?');
            if (-1 == index)
            {
                str = url;
                str2 = string.Empty;
            }
            else
            {
                str = url.Substring(0, index);
                str2 = url.Substring(index + 1);
            }
            Dictionary<string, bool> dictionary = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
            Dictionary<string, string> dictionary2 = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
            foreach (string str3 in str2.Split(new char[] { '&' }))
            {
                if (!string.IsNullOrEmpty(str3))
                {
                    string[] strArray = str3.Split(new char[] { '=' });
                    if (strArray.Length > 0)
                    {
                        string key = strArray[0];
                        if (queryStringReplacement.ContainsKey(key))
                        {
                            dictionary2[key] = queryStringReplacement[key];
                            dictionary[key] = true;
                        }
                        else
                        {
                            dictionary2[key] = (strArray.Length > 1) ? strArray[1] : string.Empty;
                        }
                    }
                }
            }
            foreach (string str5 in queryStringReplacement.Keys)
            {
                if (!dictionary.ContainsKey(str5))
                {
                    dictionary2[str5] = queryStringReplacement[str5];
                }
            }
            Dictionary<string, bool> dictionary3 = new Dictionary<string, bool>(StringComparer.OrdinalIgnoreCase);
            if (null != queryStringRemoval)
            {
                foreach (string str5 in queryStringRemoval)
                {
                    dictionary3[str5] = true;
                }
            }
            StringBuilder builder = new StringBuilder(str);
            bool flag = true;
            foreach (string str5 in dictionary2.Keys)
            {
                if (!dictionary3.ContainsKey(str5))
                {
                    if (flag)
                    {
                        builder.Append("?");
                        flag = false;
                    }
                    else
                    {
                        builder.Append("&");
                    }
                    builder.Append(str5);
                    if (!string.IsNullOrEmpty(dictionary2[str5]))
                    {
                        builder.Append("=").Append(dictionary2[str5]);
                    }
                }
            }
            return builder.ToString();
        }
    }
}

